#!/usr/bin/python3 -tt
# -*- coding: utf-8 -*-

# Python import
import sys
import argparse

# python3-gobject import
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import Gdk


class MyWindow(Gtk.ApplicationWindow):
    """
    Main Application Window
    """

    def __init__(self, app):
        Gtk.Window.__init__(self, title="Gui Test", application=app)
        self.args = app.args  # store the args from the application object
        self.set_default_size(400, 400)  # set the windows initial size

    # callback for "button"
    def on_button(self, widget, data):
        self.cnt += 1
        self.label.set_text("Button Clicked : %d" % self.cnt)
        # change label background color, just for the fun of it
        if self.args.color:
            if self.cnt % 2 == 0:
                self.label.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(255, 0, 0, 1))
            else:
                self.label.override_background_color(Gtk.StateFlags.NORMAL, None)


class MyApplication(Gtk.Application):
    '''
    Main application class
    '''

    def __init__(self):
        """
         init the parent class
         the Gio.ApplicationFlags.HANDLES_COMMAND_LINE flags tells that
         we want to handle the command line and do_command_line will be called
        """
        Gtk.Application.__init__(self, application_id="org.giuspen.CherryTree3Service", flags=Gio.ApplicationFlags.FLAGS_NONE)
        self.args = None  # store for parsed command line options
        self.window = None  # store the window
        self.path = sys.argv  # path and filename of the program executed
        print ("Filename :", self.path)  # print the filename

    def do_activate(self):
        """
        Gtk.Application activate handler
        """
        # create our main window
        self.window = MyWindow(self)
        # connect a delete_event handler ((riggered by clicking on the windows close button)
        self.window.connect('delete_event', self.on_quit)
        # show the window
        self.window.show()

    def do_startup(self):
        """
        Gtk.Application startup handler
        """
        Gtk.Application.do_startup(self)



    def do_shutdown(self):
        '''
        Gtk.Application shutdown handler
        Do clean up before the application is closed.
        this is triggered when self.quit() is called.
        '''
        Gtk.Application.do_shutdown(self)

    def on_quit(self, widget, data):
        '''
        custom quit handler
        '''
        self.quit()  # quit the application


if __name__ == '__main__':
    app = MyApplication()   # make an application object
    exit_status = app.run(sys.argv)  # run the application
    sys.exit(exit_status)  # exit with return code

